package icom.icss.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",urlPatterns = {"/shopcar.jsp"})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {

        //登录的拦截
        //如果判断是否登录，看session中有没有user
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object user = session.getAttribute("userLogin");
        if(user==null){
            //代表没登录，重定向到登录的页面
            response.sendRedirect("log_in.jsp");
            return;
        }
        //如果已经登录成功，按照过滤器的步骤执行
        filterChain.doFilter(servletRequest,servletResponse);
    }


    public void init(FilterConfig config) throws ServletException {

    }

}
